#include "user_global.h"
#include "crc.h"
#include <string.h>
#include "stdlib.h"
#include "device_usart.h"
#include "user_device.h"
#include "usart.h"
#include "user_config.h"
#include "tim.h"
#include "speed_cntr.h"
#include "modbus.h"


void HAL_UART_RxCpltCallback(UART_HandleTypeDef huart)
{
    if (huart == USART2)
    {
        if (!REN0)
            return;
        uart0_rx_pending = 1;
        if (timer_flag_get(TIMER5, TIMER_FLAG_UP))
        {
            uart0_len = 0;
        }
        timer_counter_value_config(TIMER5, 65536 - 2000);
        timer_flag_clear(TIMER5, TIMER_FLAG_UP);
        uart0_buf[UART0_BUF_MASK & uart0_len++] = (uchar)usart_data_receive(USART2);

        uart0_timestamp = timer_counter_read(TIMER5);
    }

    if (huart == USART1)
    {
        // 超时,一帧数据接收完成
        if (SystemTick() - uart1_timestamp_long >= MAX_BYTE_TO_BYTE_DELAY)
            uart1_rx_len = 0;

        uart1_rx_buf[UART1_BUF_MASK & uart1_rx_len++] = (uchar)usart_data_receive(USART1);

#ifdef BCL95
        // 判断起始字符
        if (uart1_rx_buf[UART1_BUF_MASK & (uart1_rx_len - 1)] == STX)
        {
            uart1_rx_buf[0] = STX;
            uart1_rx_len = 1;
        }
#endif
        // 判断结束字符
        if (uart1_rx_len > 5 && !check_reply)
        {
            if ((uart1_rx_buf[uart1_rx_len - 2] == CR &&
                 uart1_rx_buf[uart1_rx_len - 1] == LF))
            {
                if (uart1_rx_len < (MAX_BAR_LEN + 4))
                {
                    // uart1_rx_buf[uart1_rx_len] = 0x00;
                    Append_Bar((char *)uart1_rx_buf, uart1_rx_len - 1);
                    uart1_rx_len = 0;
                    ++ro_word.barCount;
                    Start_Beep(BEEP_INFO);
                }
                else
                {
                    uart1_rx_len = 0;
                    Start_Beep(BEEP_WARNING);
                }
            }
        }
        uart1_timestamp_long = SystemTick();
    }
}

void HAL_UART_TxCpltCallback(UART_HandleTypeDef huart)
{
    if (huart == USART2)
    {
        if (uart0_len)
        {
            usart_data_transmit(USART2, *uart0_tx_ptr++);
            --uart0_len;
        }
        else
        {
            uart0_tx_pending = 0;
            usart_interrupt_disable(USART2, USART_INT_TBE);
        }
    }

    if (huart == USART1)
    {
        if (uart1_tx_len)
        {
            usart_data_transmit(USART1, *uart1_tx_ptr++);
            --uart1_tx_len;
        }
        else
        {
            uart1_tx_pending = 0;
            uart1_rx_pending = 1;
            uart1_timestamp = SystemTickLow();
            uart1_timestamp_long = SystemTick();
            usart_interrupt_disable(USART1, USART_INT_TBE);
        }
    }
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef htim)
{
    static uint8_t i[MACHINE_COUNT] = {0};
    uint8_t role = 255;
    switch (htim)
    {
    case TIMER1:
        role = S1;
        break;
    case TIMER11:
        if (++i[S2] % 8)
            return;
        role = S2;
        break;
    case TIMER7:
        role = V2;
        break;
    case TIMER8:
        role = V3;
        break;
    case TIMER9:
        role = V1;
        break;
    case TIMER0:
        role = T1;
        break;
    case TIMER2:
        role = R1;
        break;
    case TIMER13:
        if (++i[R2] % 8)
            return;
        role = R2;
        break;
    case TIMER6:
        ++system_tick.UU16;
        break;
    default:
        break;
    }

    if (role == 255)
        return;

    speed_update(role);
}
